/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PrincipalRaccon.java
 *
 * Created on 04/06/2011, 16:32:45
 */

package com.rcn.ui;

import com.google.common.collect.Lists;
import com.rcn.dal.hibernate.GestorHibernateDAO;
import com.rcn.domain.Medico;
import com.rcn.util.UIHelper;
import com.rcn.domain.Paciente;
import com.rcn.domain.Sesion;
import com.rcn.domain.Tratamiento;
import com.rcn.exception.RaccoonException;
import com.rcn.gestores.GestorTratamiento;
import com.rcn.hibernate.GestorBD;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.joda.time.DateTime;

/**
 *
 * @author Nicolas
 */
public class PrincipalRaccon extends javax.swing.JFrame {

    //pacientes actualmente en la base de datos
    private static ArrayList<Paciente> listaPacientes;
    private boolean nuevoTratamiento=true;
    private Paciente pacienteSeleccionado;
    private Tratamiento tratamientoSeleccionado;
    //se carga en el metodo cargarSesiones() que es el que carga la tabla de sesiones
    private List<Sesion> sesionesActuales=new ArrayList<Sesion>();
    
    private List<TableModel> tablasHorarios=new ArrayList<TableModel>();
            
    /** Creates new form PrincipalRaccon */
    public PrincipalRaccon() {
        initComponents();
        this.txtFiltrarPacienteNombre.requestFocus();
        this.setIconImage(Toolkit.getDefaultToolkit().getImage("./src/main/resources/images/iconoRaccon.png"));
        setearColores();
        this.btnEditarTratamiento.setEnabled(false);
        setEnablePanelTratamiento(false);
        cargarListaPacientes();
        cargarListaMedicos();
        cargarTablasHorarios(new Date(),1);
        pacienteSeleccionado = null;
        //this.tableSesiones.getColumnModel().removeColumn(this.tableSesiones.getColumnModel().getColumn(0));
    }

    private void cargarTablasHorarios(Date fecha, int box){
        try {
            List<Sesion> sesiones = GestorHibernateDAO.getInstance().getSesionDAO().getSesiones(fecha); 
            //tablasHorarios =
        } catch (RaccoonException e) {
            JOptionPane.showMessageDialog(this, e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void setearColores(){
        //titulo tabla sesiones
        /*JTableHeader tableSesionesHeader = this.tableSesiones.getTableHeader();
        tableSesionesHeader.setForeground(new Color(0).DARK_GRAY);
        tableSesionesHeader.setBackground(new Color(204, 212, 244));
        //titulo tabla horarios
        JTableHeader tableHorariosHeader = this.tableHorarios.getTableHeader();
        tableHorariosHeader.setForeground(new Color(0).DARK_GRAY);
        tableHorariosHeader.setBackground(new Color(204, 212, 244));*/
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popMenuHorarios = new javax.swing.JPopupMenu();
        menuAgregarSesion = new javax.swing.JMenuItem();
        popGrillaSesiones = new javax.swing.JPopupMenu();
        deleteSesion = new javax.swing.JMenuItem();
        panel3 = new org.edisoncor.gui.panel.Panel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tableHorarios = new javax.swing.JTable();
        jLabel16 = new javax.swing.JLabel();
        cmbBox = new javax.swing.JComboBox();
        calendario = new com.toedter.calendar.JCalendar();
        panel4 = new org.edisoncor.gui.panel.Panel();
        btnMedicos = new org.edisoncor.gui.button.ButtonTask();
        btnCalendario = new org.edisoncor.gui.button.ButtonTask();
        btnPacientes = new org.edisoncor.gui.button.ButtonTask();
        btnSalir = new org.edisoncor.gui.button.ButtonTask();
        btnReportes = new org.edisoncor.gui.button.ButtonTask();
        panel5 = new org.edisoncor.gui.panel.Panel();
        panel1 = new org.edisoncor.gui.panel.Panel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstPacientes = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        txtFiltrarPacienteNombre = new org.edisoncor.gui.textField.TextFieldRectBackground();
        buttonAction1 = new org.edisoncor.gui.button.ButtonAction();
        btnEliminarPaciente = new org.edisoncor.gui.button.ButtonAction();
        btnEditarPaciente = new org.edisoncor.gui.button.ButtonAction();
        buttonTask1 = new org.edisoncor.gui.button.ButtonTask();
        panel2 = new org.edisoncor.gui.panel.Panel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lblNombrePaciente = new javax.swing.JLabel();
        lblApellidoPaciente = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtCantidadSesiones = new org.edisoncor.gui.textField.TextFieldRectImage();
        txtCantidadOrdenes = new org.edisoncor.gui.textField.TextFieldRectImage();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        lblApellidoMedico = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        lblNombreMedico = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        btnNuevoTratamiento = new org.edisoncor.gui.button.ButtonAction();
        btnGuardarTratamiento = new org.edisoncor.gui.button.ButtonAction();
        btnEditarTratamiento = new org.edisoncor.gui.button.ButtonAction();
        buttonTask2 = new org.edisoncor.gui.button.ButtonTask();
        jScrollPane4 = new javax.swing.JScrollPane();
        txtDiagnostico = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableSesiones = new javax.swing.JTable();
        jLabel17 = new javax.swing.JLabel();
        txtBuscarMedico = new java.awt.TextField();
        jScrollPane5 = new javax.swing.JScrollPane();
        lstMedicos = new javax.swing.JList();
        btnCancelarTratamiento = new org.edisoncor.gui.button.ButtonAction();

        popMenuHorarios.setName("popMenuHorarios");

        menuAgregarSesion.setText("Agregar Sesion");
        menuAgregarSesion.setName("menuAgregarSesion");
        menuAgregarSesion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuAgregarSesionActionPerformed(evt);
            }
        });
        popMenuHorarios.add(menuAgregarSesion);

        popGrillaSesiones.setName("popGrillaSesiones");

        deleteSesion.setText("Borrar Sesion");
        deleteSesion.setName("deleteSesion");
        deleteSesion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteSesionActionPerformed(evt);
            }
        });
        popGrillaSesiones.add(deleteSesion);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Raccon");

        panel3.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 255, 255), 2, true), "Horarios", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14))); // NOI18N
        panel3.setColorPrimario(new java.awt.Color(89, 188, 235));
        panel3.setColorSecundario(new java.awt.Color(98, 195, 241));
        panel3.setName("panel3"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        tableHorarios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"7:00-7:15", null, null},
                {"7:15-7:30", null, null},
                {"7:30-7:45", null, null},
                {"7:45-8:00", null, null},
                {"8:00-8:15", null, null},
                {"8:15-8:30", null, null},
                {"8:30-8:45", null, null},
                {"8:45-9:00", null, null},
                {"9:00-9:15", null, null},
                {"9:15-9:30", null, null},
                {"9:30-9:45", "", null},
                {"9:45-10:00", "", null},
                {"10:00-10:15", "", null},
                {"10:15-10:30", "", null},
                {"10:30-10:45", "", null},
                {"10:45-11:00", "", null},
                {"11:00-11:15", "", null},
                {"11:15-11:30", null, null},
                {"11:30-11:45", null, null},
                {"11:45-12:00", null, null},
                {"12:00-12:15", null, null},
                {"12:15-12:30", null, null},
                {"12:30-12:45", null, null},
                {"12:45-13:00", null, null},
                {"13:00-13:15", "", null},
                {"13:15-13:30", "", null},
                {"13:30-13:45", "", null},
                {"13:45-14:00", null, null}
            },
            new String [] {
                "Hora", "Paciente", "IdSesion"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                true, true, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableHorarios.setName("tableHorarios"); // NOI18N
        tableHorarios.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableHorariosMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tableHorarios);

        jLabel16.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel16.setText("Box:");
        jLabel16.setName("jLabel16"); // NOI18N

        cmbBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Box 1", "Box 2", "Box 3", "Box 4", "Box 5", "Box 6" }));
        cmbBox.setName("cmbBox"); // NOI18N

        calendario.setName("calendario"); // NOI18N

        javax.swing.GroupLayout panel3Layout = new javax.swing.GroupLayout(panel3);
        panel3.setLayout(panel3Layout);
        panel3Layout.setHorizontalGroup(
            panel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(calendario, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(panel3Layout.createSequentialGroup()
                        .addGroup(panel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(panel3Layout.createSequentialGroup()
                                .addComponent(jLabel16)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmbBox, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        panel3Layout.setVerticalGroup(
            panel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel3Layout.createSequentialGroup()
                .addComponent(calendario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(24, 24, 24)
                .addGroup(panel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(cmbBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3)
                .addContainerGap())
        );

        panel4.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255), 2), "Menu", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14))); // NOI18N
        panel4.setColorPrimario(new java.awt.Color(89, 188, 235));
        panel4.setColorSecundario(new java.awt.Color(98, 195, 241));
        panel4.setName("panel4"); // NOI18N

        btnMedicos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/medico.png"))); // NOI18N
        btnMedicos.setText("Médicos");
        btnMedicos.setDescription(" ");
        btnMedicos.setName("btnMedicos"); // NOI18N
        btnMedicos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMedicosActionPerformed(evt);
            }
        });

        btnCalendario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/calendario.png"))); // NOI18N
        btnCalendario.setText("Calendario");
        btnCalendario.setDescription(" ");
        btnCalendario.setName("btnCalendario"); // NOI18N

        btnPacientes.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/pacientes.png"))); // NOI18N
        btnPacientes.setText("Pacientes");
        btnPacientes.setDescription(" ");
        btnPacientes.setName("btnPacientes"); // NOI18N
        btnPacientes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPacientesActionPerformed(evt);
            }
        });

        btnSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/salir.png"))); // NOI18N
        btnSalir.setText("Salir");
        btnSalir.setDescription(" ");
        btnSalir.setName("btnSalir"); // NOI18N

        btnReportes.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/reportes.png"))); // NOI18N
        btnReportes.setText("Reportes");
        btnReportes.setDescription(" ");
        btnReportes.setName("btnReportes"); // NOI18N

        javax.swing.GroupLayout panel4Layout = new javax.swing.GroupLayout(panel4);
        panel4.setLayout(panel4Layout);
        panel4Layout.setHorizontalGroup(
            panel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnPacientes, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                    .addComponent(btnMedicos, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                    .addComponent(btnReportes, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                    .addComponent(btnCalendario, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel4Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(btnSalir, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))
        );
        panel4Layout.setVerticalGroup(
            panel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel4Layout.createSequentialGroup()
                .addComponent(btnPacientes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnMedicos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnReportes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCalendario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 320, Short.MAX_VALUE)
                .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        panel5.setBackground(new java.awt.Color(102, 204, 255));
        panel5.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(95, 153, 180), 2), "  Pacientes                                                                  Tratamiento                                                                           ", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(51, 51, 51))); // NOI18N
        panel5.setForeground(new java.awt.Color(102, 204, 255));
        panel5.setColorPrimario(new java.awt.Color(153, 204, 255));
        panel5.setColorSecundario(new java.awt.Color(153, 204, 255));
        panel5.setName("panel5"); // NOI18N

        panel1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(95, 153, 180), 2, true), "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14))); // NOI18N
        panel1.setColorPrimario(new java.awt.Color(209, 227, 237));
        panel1.setColorSecundario(new java.awt.Color(255, 255, 255));
        panel1.setName("panel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        lstPacientes.setName("lstPacientes"); // NOI18N
        lstPacientes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstPacientesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(lstPacientes);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("Buscar");
        jLabel1.setName("jLabel1"); // NOI18N

        txtFiltrarPacienteNombre.setDescripcion("Busqueda");
        txtFiltrarPacienteNombre.setName("txtFiltrarPacienteNombre"); // NOI18N

        buttonAction1.setText("Nuevo");
        buttonAction1.setName("buttonAction1"); // NOI18N

        btnEliminarPaciente.setText("Eliminar");
        btnEliminarPaciente.setName("btnEliminarPaciente"); // NOI18N
        btnEliminarPaciente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarPacienteActionPerformed(evt);
            }
        });

        btnEditarPaciente.setText("Editar");
        btnEditarPaciente.setName("btnEditarPaciente"); // NOI18N
        btnEditarPaciente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarPacienteActionPerformed(evt);
            }
        });

        buttonTask1.setText("Ver Historial");
        buttonTask1.setDefaultCapable(false);
        buttonTask1.setDescription("Tratamientos del Paciente");
        buttonTask1.setName("buttonTask1"); // NOI18N

        javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1);
        panel1.setLayout(panel1Layout);
        panel1Layout.setHorizontalGroup(
            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel1Layout.createSequentialGroup()
                        .addComponent(buttonAction1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEditarPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnEliminarPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtFiltrarPacienteNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(buttonTask1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        panel1Layout.setVerticalGroup(
            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel1Layout.createSequentialGroup()
                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtFiltrarPacienteNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 515, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonAction1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEliminarPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEditarPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonTask1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        panel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(95, 153, 180), 2, true), "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14))); // NOI18N
        panel2.setColorPrimario(new java.awt.Color(209, 227, 237));
        panel2.setColorSecundario(new java.awt.Color(255, 255, 255));
        panel2.setName("panel2"); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Paciente");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Nombre:");
        jLabel3.setName("jLabel3"); // NOI18N

        lblNombrePaciente.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblNombrePaciente.setName("lblNombrePaciente"); // NOI18N

        lblApellidoPaciente.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblApellidoPaciente.setName("lblApellidoPaciente"); // NOI18N

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Apellido:");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel5.setText("Sesiones");
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setText("Cantidad:");
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel7.setText("Ordenes:");
        jLabel7.setName("jLabel7"); // NOI18N

        txtCantidadSesiones.setName("txtCantidadSesiones"); // NOI18N

        txtCantidadOrdenes.setName("txtCantidadOrdenes"); // NOI18N

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/barra.jpg"))); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/barra.jpg"))); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel11.setText("Medico");
        jLabel11.setName("jLabel11"); // NOI18N

        jLabel12.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/barra.jpg"))); // NOI18N
        jLabel12.setName("jLabel12"); // NOI18N

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel13.setText("Apellido:");
        jLabel13.setName("jLabel13"); // NOI18N

        lblApellidoMedico.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblApellidoMedico.setName("lblApellidoMedico"); // NOI18N

        jLabel14.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel14.setText("Nombre:");
        jLabel14.setName("jLabel14"); // NOI18N

        lblNombreMedico.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblNombreMedico.setName("lblNombreMedico"); // NOI18N

        jLabel15.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel15.setText("Diagnóstico:");
        jLabel15.setName("jLabel15"); // NOI18N

        btnNuevoTratamiento.setText("Nuevo");
        btnNuevoTratamiento.setName("btnNuevoTratamiento"); // NOI18N
        btnNuevoTratamiento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoTratamientoActionPerformed(evt);
            }
        });

        btnGuardarTratamiento.setText("Guardar");
        btnGuardarTratamiento.setName("btnGuardarTratamiento"); // NOI18N
        btnGuardarTratamiento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarTratamientoActionPerformed(evt);
            }
        });

        btnEditarTratamiento.setText("Editar");
        btnEditarTratamiento.setName("btnEditarTratamiento"); // NOI18N
        btnEditarTratamiento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarTratamientoActionPerformed(evt);
            }
        });

        buttonTask2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/guardar.png"))); // NOI18N
        buttonTask2.setText("Finalizar Tratamiento");
        buttonTask2.setDescription("Guardar");
        buttonTask2.setName("buttonTask2"); // NOI18N

        jScrollPane4.setName("jScrollPane4"); // NOI18N

        txtDiagnostico.setColumns(20);
        txtDiagnostico.setRows(5);
        txtDiagnostico.setName("txtDiagnostico"); // NOI18N
        jScrollPane4.setViewportView(txtDiagnostico);

        jScrollPane2.setName("jScrollPane2");

        tableSesiones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "id", "Asistio", "Orden", "Fecha", "Hora", "Box"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Long.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, true, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableSesiones.setName("tableSesiones");
        tableSesiones.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableSesionesMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tableSesiones);

        jLabel17.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel17.setText("Buscar:");
        jLabel17.setName("jLabel17");

        txtBuscarMedico.setName("txtBuscarMedico");

        jScrollPane5.setName("jScrollPane5");

        lstMedicos.setName("lstMedicos");
        lstMedicos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstMedicosMouseClicked(evt);
            }
        });
        jScrollPane5.setViewportView(lstMedicos);

        btnCancelarTratamiento.setText("Cancelar");
        btnCancelarTratamiento.setName("btnCancelarTratamiento");
        btnCancelarTratamiento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarTratamientoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panel2Layout = new javax.swing.GroupLayout(panel2);
        panel2.setLayout(panel2Layout);
        panel2Layout.setHorizontalGroup(
            panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panel2Layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblApellidoPaciente)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3)
                        .addGap(33, 33, 33)
                        .addComponent(lblNombrePaciente)
                        .addGap(72, 72, 72))
                    .addComponent(jLabel12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(panel2Layout.createSequentialGroup()
                        .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(jLabel2)
                            .addComponent(buttonTask2, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(panel2Layout.createSequentialGroup()
                                .addGap(33, 33, 33)
                                .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(panel2Layout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addComponent(jLabel17)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtBuscarMedico, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(panel2Layout.createSequentialGroup()
                                        .addComponent(jLabel13)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(lblApellidoMedico)
                                        .addGap(62, 62, 62)
                                        .addComponent(jLabel14)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(lblNombreMedico))))
                            .addGroup(panel2Layout.createSequentialGroup()
                                .addComponent(jLabel15)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE)
                                    .addComponent(jScrollPane4))))
                        .addContainerGap())
                    .addGroup(panel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                        .addContainerGap())
                    .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(panel2Layout.createSequentialGroup()
                        .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(panel2Layout.createSequentialGroup()
                                .addComponent(btnNuevoTratamiento, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnEditarTratamiento, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnGuardarTratamiento, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnCancelarTratamiento, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(panel2Layout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtCantidadSesiones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(19, 19, 19)
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtCantidadOrdenes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel5))
                        .addGap(0, 1, Short.MAX_VALUE))))
        );
        panel2Layout.setVerticalGroup(
            panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel2Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addGap(3, 3, 3)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblApellidoPaciente)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(lblNombrePaciente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 4, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel13)
                        .addComponent(lblApellidoMedico))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel2Layout.createSequentialGroup()
                        .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblNombreMedico)
                            .addComponent(jLabel14))
                        .addGap(2, 2, 2)))
                .addGap(14, 14, 14)
                .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel17)
                    .addComponent(txtBuscarMedico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel15)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 4, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel7)
                        .addComponent(txtCantidadOrdenes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtCantidadSesiones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEditarTratamiento, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnGuardarTratamiento, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnNuevoTratamiento, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCancelarTratamiento, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonTask2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(62, 62, 62))
        );

        javax.swing.GroupLayout panel5Layout = new javax.swing.GroupLayout(panel5);
        panel5.setLayout(panel5Layout);
        panel5Layout.setHorizontalGroup(
            panel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel5Layout.createSequentialGroup()
                .addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(panel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        panel5Layout.setVerticalGroup(
            panel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel5Layout.createSequentialGroup()
                .addGroup(panel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(panel2, javax.swing.GroupLayout.PREFERRED_SIZE, 671, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, 671, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(panel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE, false)
                    .addComponent(panel5, javax.swing.GroupLayout.PREFERRED_SIZE, 695, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(panel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(panel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnPacientesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPacientesActionPerformed
        abrirVentanaPacientes(getNewAbmPaciente(null));
    }//GEN-LAST:event_btnPacientesActionPerformed
    
    public AbmPaciente getNewAbmPaciente(Paciente paciente){
        AbmPaciente abmPaciente=new AbmPaciente(this, true,paciente);
        abmPaciente.setLocationRelativeTo(null);
        return abmPaciente;
    }
    public AbmPaciente abrirVentanaPacientes( AbmPaciente abmPaciente){
        abmPaciente.setVisible(true);
        return abmPaciente;
    }
    private void btnMedicosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMedicosActionPerformed
        AbmMedico abmMedico=new AbmMedico(this, true);
        abmMedico.setLocationRelativeTo(null);
        abmMedico.setVisible(true);
    }//GEN-LAST:event_btnMedicosActionPerformed

    private void lstPacientesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstPacientesMouseClicked
        limpiarPanelTratamiento();
        setEnablePanelTratamiento(false);
        pacienteSeleccionado=UIHelper.getInstance().getPacienteFromList(lstPacientes);
        setPaciente(pacienteSeleccionado);
    }//GEN-LAST:event_lstPacientesMouseClicked

    private void btnEliminarPacienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarPacienteActionPerformed
        try {
            UIHelper.getInstance().eliminarPaciente(lstPacientes, listaPacientes);
        } catch (RaccoonException e) {
            JOptionPane.showMessageDialog(this, e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
            return;
        } 
        cargarListaPacientes(); //para no tener que traer todos los pac de la bd
        JOptionPane.showMessageDialog(this, "El paciente se elimino con éxito","Información",JOptionPane.INFORMATION_MESSAGE);
        this.setPaciente(null);
    }//GEN-LAST:event_btnEliminarPacienteActionPerformed

    private void btnEditarPacienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarPacienteActionPerformed
        AbmPaciente abmPaciente= getNewAbmPaciente(UIHelper.getInstance().getPacienteFromList(lstPacientes));
        abmPaciente.editarPaciente();
        abrirVentanaPacientes(abmPaciente);
    }//GEN-LAST:event_btnEditarPacienteActionPerformed

    private void btnEditarTratamientoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarTratamientoActionPerformed
        nuevoTratamiento=false;
        setEnablePanelTratamiento(true);
    }//GEN-LAST:event_btnEditarTratamientoActionPerformed

    private void btnNuevoTratamientoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoTratamientoActionPerformed
        nuevoTratamiento=true;
        sesionesActuales=new ArrayList<Sesion>();
        setEnablePanelTratamiento(true);
    }//GEN-LAST:event_btnNuevoTratamientoActionPerformed

    private void btnGuardarTratamientoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarTratamientoActionPerformed
        String mensajeConfirmacion="guardó";
        try{ 
            Paciente paciente=UIHelper.getInstance().getPacienteFromList(lstPacientes);
            
            Medico medico=UIHelper.getInstance().getMedicoFromList(lstMedicos);
            if(medico==null){
                throw new RaccoonException("Debe seleccionar un medico.", null);
            }
             Tratamiento tratamiento=null;
            if(nuevoTratamiento){
               tratamiento=new Tratamiento();
            }
            else{
                mensajeConfirmacion="actualizó";
                List<Tratamiento> tratamientos=GestorHibernateDAO.getInstance().getTratamientoDAO().getTratamientosDePaciente(paciente);
                tratamiento=tratamientos.get(0);//TODO manejar varios tratamientos
                tratamiento.setDiagnostico(txtDiagnostico.getText());
            }
            
            tratamiento.setMedico(medico);

            if(paciente==null){
                //no deberia entrar nunca aca, pero just in case
                throw new RaccoonException("Debe seleccionar un paciente.", null);
            }
            tratamiento.setPaciente(paciente);

            tratamiento.setActivo(true);
            // tratamiento.
            tratamiento.setDiagnostico(txtDiagnostico.getText());

            tratamiento.setSesiones(new ArrayList<Sesion>());
            
            for(Sesion sesion:sesionesActuales){
                tratamiento.getSesiones().add(sesion);
            }

            GestorHibernateDAO.getInstance().getTratamientoDAO().saveOrUpdate(tratamiento);
   
        }catch(RaccoonException e){
             JOptionPane.showMessageDialog(this, e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
             return;
        }catch(Exception e){
             JOptionPane.showMessageDialog(this, e.getMessage(),"Error Inesperado",JOptionPane.ERROR_MESSAGE);
             return;
        }
        JOptionPane.showMessageDialog(this, "El tratamiento se "+mensajeConfirmacion+" con éxito","Información",JOptionPane.INFORMATION_MESSAGE);
        this.setEnablePanelTratamiento(false);
    }//GEN-LAST:event_btnGuardarTratamientoActionPerformed

    private void tableHorariosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableHorariosMouseClicked
        if(SwingUtilities.isRightMouseButton(evt)){
            this.popMenuHorarios.show(this.tableHorarios, evt.getX(), evt.getY());
        }
    }//GEN-LAST:event_tableHorariosMouseClicked

    private void lstMedicosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstMedicosMouseClicked
       Medico medicoSeleccionado=UIHelper.getInstance().getMedicoFromList(lstMedicos);
       this.setMedico(medicoSeleccionado);
    }//GEN-LAST:event_lstMedicosMouseClicked

    private void menuAgregarSesionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuAgregarSesionActionPerformed
        int[] rowSelected = tableHorarios.getSelectedRows();  
        for (int row:rowSelected) {
            String value=(String)tableHorarios.getModel().getValueAt(row, 1);
            if(value != null && !value.isEmpty()){
                 JOptionPane.showMessageDialog(this, "Horario ocupado.","Error",JOptionPane.ERROR_MESSAGE);
                 return;
            }
        } 
        Sesion sesion=getNuevaSesion();
        cargarSesion(sesion);
    }//GEN-LAST:event_menuAgregarSesionActionPerformed

    private void btnCancelarTratamientoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarTratamientoActionPerformed
        
    }//GEN-LAST:event_btnCancelarTratamientoActionPerformed

    private void deleteSesionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteSesionActionPerformed
        int rowSelected = tableSesiones.getSelectedRow();  
        String value=(String)tableSesiones.getModel().getValueAt(rowSelected, 3);
        try{
            if(value != null && !value.isEmpty()){
                Long idSesion=(Long)tableSesiones.getModel().getValueAt(rowSelected, 0);
                borrarSesion(tratamientoSeleccionado, idSesion);
            }
        }
        catch(RaccoonException e){
            JOptionPane.showMessageDialog(this, e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
        } 
        
    }//GEN-LAST:event_deleteSesionActionPerformed

    private void tableSesionesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableSesionesMouseClicked
       if(SwingUtilities.isRightMouseButton(evt)){
            this.popGrillaSesiones.show(this.tableSesiones, evt.getX(), evt.getY());
        }
    }//GEN-LAST:event_tableSesionesMouseClicked
   
    
    private void actualizarCantidadDeSesionesOrdenes(){
         List<Sesion> sesiones=this.sesionesActuales;
         Iterator iterator = sesiones.iterator();
         int cantSesiones=0;
         int cantOrdenes=0;
         while(iterator.hasNext()){
             cantSesiones++;
             Sesion sesion=(Sesion)iterator.next();
             if(sesion.isOrden()){
                 cantOrdenes++;
             }
         }
         txtCantidadSesiones.setText(Integer.toString(cantSesiones));
         txtCantidadOrdenes.setText(Integer.toString(cantOrdenes));
    }

    /**
     * Carga un paciente en los campos del panel de paciente.
     * @param paciente
     */
    private void setPaciente(Paciente paciente){
        if(paciente!=null){
            this.lblApellidoPaciente.setText(paciente.getApellido());
            this.lblNombrePaciente.setText(paciente.getNombre());   
            cargarTratamientoActual();
        } 
    }
    
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                PrincipalRaccon raccoon=new PrincipalRaccon();
                raccoon.setLocationRelativeTo(null);
                raccoon.setVisible(true);
            }
        });
    }

    /**
     * Carga en el Jlist una lista de pacientes.
     */
    private void cargarListaPacientes(){
        try{
            listaPacientes=(ArrayList<Paciente>)GestorHibernateDAO.getInstance().getPacienteDAO().getAll(Paciente.class);
            UIHelper.getInstance().cargarListaPacientes(this.lstPacientes, listaPacientes);
        }catch(RaccoonException e){
            JOptionPane.showMessageDialog(this, e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
        } 
    }
    
    private Paciente getPaciente(){
         Paciente paciente = UIHelper.getInstance().getPacienteFromList(lstPacientes);
         return paciente;
    }
    
    private void cargarTratamientoActual(){
        Paciente paciente = getPaciente();
        try {
            this.btnEditarTratamiento.setEnabled(false);
            List<Tratamiento> tratamientos = GestorTratamiento.getInstance().getTratamientoActual(paciente);
            if(tratamientos!=null){
                this.btnEditarTratamiento.setEnabled(true);
                tratamientoSeleccionado = tratamientos.get(0);
                cargarDatosTratamiento(tratamientoSeleccionado);//cargo el primer activo, falta el manejo de varios tratamientos activos
            }
        } catch (RaccoonException e) {
            JOptionPane.showMessageDialog(this, e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void cargarDatosTratamiento(Tratamiento tratamiento) throws RaccoonException{
        if(tratamiento!=null){
            this.setMedico(tratamiento.getMedico());
            txtDiagnostico.setText(tratamiento.getDiagnostico());
            this.cargarSesiones(Lists.newArrayList(tratamiento.getSesiones()));
        } 
    }
    
    private void limpiarPanelTratamiento(){
        lblNombrePaciente.setText("");
        lblNombreMedico.setText("");
        lblApellidoMedico.setText("");
        lblApellidoPaciente.setText("");
        txtCantidadOrdenes.setText("");
        txtCantidadSesiones.setText("");
        txtDiagnostico.setText("");
        DefaultTableModel dm = (DefaultTableModel)tableSesiones.getModel();
        dm.getDataVector().removeAllElements();
        dm.fireTableDataChanged();
    }
    
    private void setEnablePanelTratamiento(boolean b){
        txtDiagnostico.setEnabled(b);
        txtCantidadOrdenes.setEnabled(b);
        txtCantidadSesiones.setEnabled(b);
        tableSesiones.setEnabled(b);
        lstMedicos.setEnabled(b);
        txtBuscarMedico.setEnabled(b);
        tableHorarios.setEnabled(b);
    }
    
    private void cargarSesiones(List<Sesion> sesiones){
        if(sesiones!=null){
            this.sesionesActuales=sesiones;
            this.tableSesiones.removeAll();          
            for (Sesion sesion : sesiones) {
                cargarSesion(sesion);
            }
        }
    }
    
    /**
     * agrega una sesion a la tabla de sesiones
     * @param sesion 
     */
    private void cargarSesion(Sesion sesion){
        DefaultTableModel model=(DefaultTableModel)this.tableSesiones.getModel();
        if(sesion!=null){
            Object[] sesionValues=new Object[6];
            sesionValues[0] = sesion.getId(); //columna invisible
            sesionValues[1] = sesion.isSesionDisponible();
            sesionValues[2] = sesion.isOrden();       
            Format df = DateFormat.getDateInstance(DateFormat.SHORT);
            sesionValues[3] = df.format(sesion.getFechaPactadaEntrada());
            sesionValues[4] = sesion.getIntervaloSesion();
            sesionValues[5] = sesion.getBox();
            model.addRow(sesionValues);
            
            //Agrega nombre paciente en la grilla de turnos
            int[] rowSelected = tableHorarios.getSelectedRows();  
            for (int row:rowSelected) {
                 tableHorarios.getModel().setValueAt(getPaciente().toString(), row, 1);
            }
            if(!sesionesActuales.contains(sesion)){
                sesionesActuales.add(sesion);
            }
        }
    }
    
    private void borrarSesion(Tratamiento t, Long idSesion) throws RaccoonException{
        t.borrarSesion(idSesion);
        GestorHibernateDAO.getInstance().getTratamientoDAO().saveOrUpdate(t);
        this.tableSesiones.remove(this.tableSesiones.getSelectedRow());
    }
    
    private void cargarListaMedicos(){
        try{
            List<Medico> listaMedicos=(ArrayList<Medico>)GestorHibernateDAO.getInstance().getMedicoDAO().getAll(Medico.class);
            UIHelper.getInstance().cargarListaMedicos(this.lstMedicos, listaMedicos);
        }catch(RaccoonException e){
            JOptionPane.showMessageDialog(this, e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
        } 
    }
    
    private void setMedico(Medico medico) {
        if(medico!=null){
            this.lstMedicos.setSelectedValue(medico, true);
            this.lblApellidoMedico.setText(medico.getApellido());
            this.lblNombreMedico.setText(medico.getNombre()); 
        }
    }
    
    /**
     * Retorna una sesion en base a la seleccion de fecha,
     * box, y hora del panel de la derecha.
     * @return 
     */
    private Sesion getNuevaSesion(){
        Sesion sesion=null;
        int [] rowSelected = tableHorarios.getSelectedRows();
        Object entrada = tableHorarios.getModel().getValueAt(rowSelected[0], 0);
        Object salida = tableHorarios.getModel().getValueAt(rowSelected[rowSelected.length - 1], 0);
        if(entrada!=null && salida != null){
            
            sesion=new Sesion();
            sesion.setBox(cmbBox.getSelectedIndex()+1);//TODO FALTA MANEJO DE BOX
            
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(this.calendario.getCalendar().getTime()); //set fecha
            
            /**
             * Hago dos splits porque el primero separa el intervalo.
             * Ejemplo 7:00 - 7:15. Separo por "-" y luego al primer valor (7:00)
             * le hago un split por ":" para obtener la hora por un lado 
             * y los minutos por el otro
             */
            //Hora entrada
            String [] time = ((String)entrada).split("-");
            time = time[0].split(":");
            calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0])); //set hora entrada
            calendar.set(Calendar.MINUTE, Integer.parseInt(time[1])); // set mins entrada
            calendar.set(Calendar.SECOND, 0);
            sesion.setFechaPactadaEntrada(calendar.getTime());
            //Hora salida
            time = ((String)salida).split("-");
            time = time[1].split(":");
            calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0])); //set hora salida
            calendar.set(Calendar.MINUTE, Integer.parseInt(time[1])); // set mins salida
            calendar.set(Calendar.SECOND, 0);
            sesion.setFechaPactadaSalida(calendar.getTime());
            
            sesion.setOrden(false);
            sesion.setSesionDisponible(false);
        }
        return sesion;
    }
    
    private String getHoraSeleccionada(){
        int[] rowSelected = tableHorarios.getSelectedRows();

        String[] inicio=((String)tableHorarios.getModel().getValueAt(rowSelected[0], 0)).split("-");
        
        String[] fin=((String)tableHorarios.getModel().getValueAt(rowSelected[rowSelected.length-1], 0)).split("-");
        return inicio[0]+"-"+fin[1];
    }
    
    public JList getLstPacientes(){
        return lstPacientes;
    }
    
    public JList getLstMedicos(){
        return lstMedicos;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.edisoncor.gui.button.ButtonTask btnCalendario;
    private org.edisoncor.gui.button.ButtonAction btnCancelarTratamiento;
    private org.edisoncor.gui.button.ButtonAction btnEditarPaciente;
    private org.edisoncor.gui.button.ButtonAction btnEditarTratamiento;
    private org.edisoncor.gui.button.ButtonAction btnEliminarPaciente;
    private org.edisoncor.gui.button.ButtonAction btnGuardarTratamiento;
    private org.edisoncor.gui.button.ButtonTask btnMedicos;
    private org.edisoncor.gui.button.ButtonAction btnNuevoTratamiento;
    private org.edisoncor.gui.button.ButtonTask btnPacientes;
    private org.edisoncor.gui.button.ButtonTask btnReportes;
    private org.edisoncor.gui.button.ButtonTask btnSalir;
    private org.edisoncor.gui.button.ButtonAction buttonAction1;
    private org.edisoncor.gui.button.ButtonTask buttonTask1;
    private org.edisoncor.gui.button.ButtonTask buttonTask2;
    private com.toedter.calendar.JCalendar calendario;
    private javax.swing.JComboBox cmbBox;
    private javax.swing.JMenuItem deleteSesion;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JLabel lblApellidoMedico;
    private javax.swing.JLabel lblApellidoPaciente;
    private javax.swing.JLabel lblNombreMedico;
    private javax.swing.JLabel lblNombrePaciente;
    private javax.swing.JList lstMedicos;
    private javax.swing.JList lstPacientes;
    private javax.swing.JMenuItem menuAgregarSesion;
    private org.edisoncor.gui.panel.Panel panel1;
    private org.edisoncor.gui.panel.Panel panel2;
    private org.edisoncor.gui.panel.Panel panel3;
    private org.edisoncor.gui.panel.Panel panel4;
    private org.edisoncor.gui.panel.Panel panel5;
    private javax.swing.JPopupMenu popGrillaSesiones;
    private javax.swing.JPopupMenu popMenuHorarios;
    private javax.swing.JTable tableHorarios;
    private javax.swing.JTable tableSesiones;
    private java.awt.TextField txtBuscarMedico;
    private org.edisoncor.gui.textField.TextFieldRectImage txtCantidadOrdenes;
    private org.edisoncor.gui.textField.TextFieldRectImage txtCantidadSesiones;
    private javax.swing.JTextArea txtDiagnostico;
    private org.edisoncor.gui.textField.TextFieldRectBackground txtFiltrarPacienteNombre;
    // End of variables declaration//GEN-END:variables
  
}
